package cn.com.pusai.core.util;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.sql.Date;
import java.sql.Timestamp;

/**
 * 对字符串的操作
 * */
public class StringUtil
{

	public StringUtil()
	{
	}
	
	/**
	 * 字符串编码
	 * @param str 源字符串
	 * @param encoding 编码方式 如:utf-8
	 * */
	public static String encodeURIComponent(String str, String encoding)
	{
		try{
		if (StringUtil.isNotEmpty(str.trim()))

		return URLEncoder.encode(str, encoding);
		
		
		}catch(UnsupportedEncodingException ex){
			ex.printStackTrace();
			return str;
		}
		return "";
	}
	
	/**
	 * 字符串编号(默认格式:utf-8)
	 * @param str 源字符串
	 * */
	public static String encodeURIComponent(String str)
	{
		return encodeURIComponent(str, "UTF-8");
	}
	
	/**
	 * 替换包含的特殊字符  " < > '
	 * @param 字符串
	 * */
	public static String escapeXml(String str)
	{
		if (str == null)
			return "";
		StringBuilder sb = new StringBuilder();
		int len = str.length();
		for (int i = 0; i < len; i++)
		{
			char c = str.charAt(i);
			if (c == '"')
				sb.append("&quot;");
			else
			if (c == '&')
				sb.append("&amp;");
			else
			if (c == '<')
				sb.append("&lt;");
			else
			if (c == '>')
				sb.append("&gt;");
			else
			if (c == '\'')
				sb.append("&apos;");
			else
				sb.append(c);
		}

		return sb.toString();
	}

	public static String literalHtml(String str)
	{
		if (str == null)
			return "";
		str = escapeXml(str);
		StringBuilder sb = new StringBuilder();
		int len = str.length();
		for (int i = 0; i < len; i++)
		{
			char c = str.charAt(i);
			if (c == ' ')
				sb.append("&#160;");
			else
			if (c == '\t')
				sb.append("&#160;&#160;&#160;&#160;");
			else
			if (c != '\r')
				sb.append(c);
		}

		str = StringUtil.replace(sb.toString(), "\n", "<br />");
		return str;
	}
	
	public static boolean isEmpty(String str)
	{
		return str == null || str.length() == 0;
	}

	public static boolean isNotEmpty(String str)
	{
		return !isEmpty(str);
	}

	public static boolean isBlank(String str)
	{
		int strLen;
		if (str == null || (strLen = str.length()) == 0)
			return true;
		for (int i = 0; i < strLen; i++)
			if (!Character.isWhitespace(str.charAt(i)))
				return false;

		return true;
	}

	public static boolean isNotBlank(String str)
	{
		return !isBlank(str);
	}

	public static String trim(String str)
	{
		return str != null ? str.trim() : null;
	}

	public static String trimToNull(String str)
	{
		String ts = trim(str);
		return isEmpty(ts) ? null : ts;
	}

	public static String trimToEmpty(String str)
	{
		return str != null ? str.trim() : "";
	}

	public static boolean equals(String str1, String str2)
	{
		return str1 != null ? str1.equals(str2) : str2 == null;
	}

	public static boolean equalsIgnoreCase(String str1, String str2)
	{
		return str1 != null ? str1.equalsIgnoreCase(str2) : str2 == null;
	}

	public static String replace(String text, String searchString, String replacement)
	{
		return replace(text, searchString, replacement, -1);
	}

	public static String replace(String text, String searchString, String replacement, int max)
	{
		if (isEmpty(text) || isEmpty(searchString) || replacement == null || max == 0)
			return text;
		int start = 0;
		int end = text.indexOf(searchString, start);
		if (end == -1)
			return text;
		int replLength = searchString.length();
		int increase = replacement.length() - replLength;
		increase = increase >= 0 ? increase : 0;
		increase *= max >= 0 ? max <= 64 ? max : 64 : 16;
		StringBuffer buf = new StringBuffer(text.length() + increase);
		for (; end != -1; end = text.indexOf(searchString, start))
		{
			buf.append(text.substring(start, end)).append(replacement);
			start = end + replLength;
			if (--max == 0)
				break;
		}

		buf.append(text.substring(start));
		return buf.toString();
	}

	public static String remove(String str, String remove)
	{
		if (isEmpty(str) || isEmpty(remove))
			return str;
		else
			return replace(str, remove, "", -1);
	}

	public static String abbr(String str, int maxWidth)
	{
		return abbreviate(str, maxWidth);
	}

	public static String abbreviate(String str, int maxWidth)
	{
		return abbreviate(str, 0, maxWidth);
	}

	public static String getStringValue(Object obj, String formate, String defaultValue)
		throws Exception
	{
//		if (obj == null)
//			return defaultValue;
//		switch (ClassTypeUtil.getTypeByClass(obj.getClass()))
//		{
//		case -1: 
//			throw new Exception((new StringBuilder(String.valueOf(obj.getClass().getName()))).append("类型未在dcore中定义！").toString());
//
//		case 7: // '\007'
//			return DateUtil.format((Timestamp)obj, formate);
//
//		case 6: // '\006'
//			return DateUtil.format((Date)obj, formate);
//
//		case 10: // '\n'
//			return DateUtil.format((java.util.Date)obj, formate);
//		}
		return obj.toString();
	}

	private static String abbreviate(String str, int offset, int maxWidth)
	{
		if (str == null)
			return null;
		if (maxWidth < 4)
			throw new IllegalArgumentException("maxWidth的值最小必须为4");
		if (str.length() <= maxWidth)
			return str;
		if (offset > str.length())
			offset = str.length();
		if (str.length() - offset < maxWidth - 3)
			offset = str.length() - (maxWidth - 3);
		if (offset <= 4)
			return (new StringBuilder(String.valueOf(str.substring(0, maxWidth - 3)))).append("...").toString();
		if (maxWidth < 7)
			throw new IllegalArgumentException("Minimum abbreviation width with offset is 7");
		if (offset + (maxWidth - 3) < str.length())
			return (new StringBuilder("...")).append(abbreviate(str.substring(offset), maxWidth - 3)).toString();
		else
			return (new StringBuilder("...")).append(str.substring(str.length() - (maxWidth - 3))).toString();
	}
}
